<template>
	<view class="recharge">
		<view class="remainder">
			<image class="remainderbg" src="../../static/recharge/bg@2x.png" mode="aspectFill"></image>
			我的剩余次数
			<view class="remain_num">
				{{total_money||0}}
			</view>
		</view>
		<view class="color-ffffff title">
			会员充值
		</view>
		<view class="recharge_view">
			<view v-for="(vip,i) in vipList" :class="'recharge_card '+ (i==activeIndex?'active':'')"
				@click="()=>{ activeIndex = i }">
				<view>
					按次计费
				</view>
				<view>
					{{vip.name}}
				</view>
				<view>
					{{vip.price}}
				</view>
				<view>
					内含{{vip.num}}次对话
					<view>
						30天内有效
					</view>
				</view>
			</view>
		</view>
		<view class="recharge_submit" @click="goPayOrder()">
			立即升级
		</view>
		<view style="position: relative;">
			<by-nike-avtar ref="login"></by-nike-avtar>
		</view>
	</view>
</template>

<script>
	import {
		payOrder
	} from '../../API/payOrder.js'
	import {
		getVipList
	} from '../../API/getVipList.js'
	import {
		getUserInfo
	} from '../../API/getUserInfo.js'
	import userStore from '../../utils/userStore.js'
	export default {
		data() {
			return {
				total_money: 0,
				vipList: [],
				activeIndex: -1,
			}
		},
		async onShow() {
			// await getUserInfo();
			this.total_money = userStore.get().total_money;
			this.vipList = await getVipList();
			console.log(this.vipList)
		},
		methods: {
			async goPayOrder() {
				let _this = this;
				if (this.activeIndex == -1) {
					uni.showToast({
						title: "请选择套餐",
						icon: 'none'
					})
					return;
				};
				uni.showLoading({
					title:'加载中...'
				})
				let res = await payOrder({
					vip_id: this.vipList[this.activeIndex].id
				});
				uni.hideLoading();
				console.log(JSON.parse(JSON.stringify(res)));
				uni.requestPayment({
					provider: "wxpay", //固定值为"wxpay"
					timeStamp: res.timeStamp,
					nonceStr: res.nonceStr,
					package: res.package,
					signType: 'RSA',
					paySign: res.paySign,
					success: async function(res) {
						// var rawdata = JSON.parse(res.rawdata);
						console.log("支付成功");
						await getUserInfo();
						_this.total_money = userStore.get().total_money;
					},
					fail: function(err) {
						console.log('支付失败:' + JSON.stringify(err));
					}
				});

			}
		}
	}
</script>

<style scoped lang="scss">
	.recharge {
		min-height: 100vh;
		background: linear-gradient(0deg, #CDF1F7, #BCDFFD, #CFDBFF);
	}

	.remainder {
		width: 688upx;
		height: 218upx;
		margin: 0 auto;
		position: relative;
		padding: 36upx 0 39upx 0;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
		gap: 30upx;
		color: #fff;
	}

	.remain_num {
		font-size: 80upx;
		line-height: 80upx;
	}

	.remainderbg {
		position: absolute;
		width: 100%;
		height: 100%;
	}

	.recharge_view {
		display: flex;
		padding: 0 29upx;
		flex-wrap: wrap;
		gap: 40upx;
	}

	.recharge_card.active {

		background: #F8F0E7;
		border: 1px solid #DAA64A;
	}

	.recharge_card {
		width: 201upx;
		height: 251upx;
		background: #ffffff;
		border: 1px solid #ffffff;
		border-radius: 20upx;
		display: flex;
		align-items: center;
		flex-direction: column;
		overflow: hidden;

		&>view:first-child {
			width: 100%;
			height: 30upx;
			text-align: center;
			background: #333333;
			font-size: 20upx;
			color: #ffffff;
		}

		&>view:nth-child(2) {
			margin-top: 24upx;
			font-size: 24upx;
			white-space: nowrap;
			font-weight: 400;
			color: #333333;
		}

		&>view:nth-child(3) {
			margin-top: 17upx;
			font-size: 40upx;
			font-weight: 400;
			color: #DAA64A;
		}

		&>view:nth-child(4) {
			margin-top: 22upx;
			font-size: 22upx;
			line-height: 25upx;
			text-align: center;
			font-weight: 400;
			color: #808080;
		}
	}

	.recharge_submit {
		margin: 0 auto;
		margin-top: 68upx;
		width: 645upx;
		height: 90upx;
		background: #3E80E5;
		color: #fff;
		border-radius: 45upx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	/*  */
	.color-ffffff {
		color: #fff;
	}

	.title {
		padding: 30upx;
		font-size: 34upx;
	}
</style>